package com.woniuxy.controller;

import com.woniuxy.entity.User;
import com.woniuxy.service.LoginService;
import com.woniuxy.utils.JwtUtil;
import com.woniuxy.utils.RedisUtil;
import com.woniuxy.utils.ResponseResult;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/login")
public class Login {
    @Autowired
    private LoginService loginService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RabbitTemplate rabbitTemplate;




    @RequestMapping("/numberAndPassword")
    public ResponseResult login(@RequestParam(value = "account") String account, @RequestParam(value = "password") String password,
                                HttpServletResponse response) {
        System.out.println("username: " + account + ", password: " + password);
        User user = loginService.login(account, password);
        System.out.println("user: " + user);
        if (user != null) {
            //登录成功,生成token，添加到请求头，响应给前端
            String token = JwtUtil.create(user.getUserId().toString(), user.getAccount());
            System.out.println("token: " + token);
            response.setHeader("token",token);
            //前后分离程序，必须暴露响应头，否则无法获取响应头
            response.setHeader("Access-Control-Expose-Headers","token");
            return ResponseResult.success(user);
        }else {
            return ResponseResult.fail(400, "用户名或密码错误");
        }

    }


    @GetMapping("/authCode")
    public ResponseResult authCode(@RequestParam(value = "email") String email) {
        rabbitTemplate.convertAndSend("email-exchange","", email);
        System.out.println("email: " + email);
        return ResponseResult.success();

    }

    @GetMapping("/emailLogin")
    public ResponseResult emailLogin(@RequestParam(value = "email") String email, @RequestParam(value = "code") String code,HttpServletResponse response) {
        User user = loginService.getUserByEmail(email);
        System.out.println("code: " + code);
        System.out.println("redis code: " + redisUtil.get(email));
        if(code.equals(redisUtil.get(email))){
            redisUtil.delete(email);
            if (user != null) {
                //登录成功,生成token，添加到请求头，响应给前端
                String token = JwtUtil.create(user.getUserId().toString(), user.getAccount());
                response.setHeader("token",token);
                //前后分离程序，必须暴露响应头，否则无法获取响应头
                response.setHeader("Access-Control-Expose-Headers","token");

                return ResponseResult.success(user);
            }else {
                return ResponseResult.fail(400, "邮箱不存在");
            }
        }else{
            return ResponseResult.fail(400, "验证码错误");
        }



    }




}

